import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useAuthorizationStore } from '@/service/AuthorizationService'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/history',
      name: 'history',
      component: () => import('../views/HistoryView.vue')
    },
    {
      path: '/file',
      name: 'file',
      component: () => import('../views/FileView.vue')
    },
    {
      path: '/statistics',
      name: 'statistics',
      component: () => import('../views/StatisticsView.vue')
    },
    {
      path: '/log',
      name: 'log',
      component: () => import('../views/LogView.vue')
    },
    {
      path: '/change',
      name: 'change',
      component: () => import('../views/ChangeView.vue')
    }
  ]
})

export const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async (to) => {
  const authorizationStore = useAuthorizationStore()
  if (whiteList.indexOf(to.path) !== -1) return true
  if (authorizationStore.isAuthenticated()) {
    return true
  } else {
    return '/login'
  }
})

export default router
